package org.learning;

//jvm 只会等待非守护线程执行完成才会自动退出，守护线程也会自动结束
public class A_06 {
    private static class MyDaemonThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 500; i++) {
                System.out.println("MyDaemonThread is running");
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class MyThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println("MyThread is running");
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static class MyThread2 extends Thread {
        private boolean isStop = false;

        @Override
        public void run() {
            while (!isStop) {
                System.out.println("MyThread2 is running");
            }
        }

        public void stopThread() {
            isStop = true;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyDaemonThread myDaemonThread = new MyDaemonThread();
        myDaemonThread.setDaemon(true);
        myDaemonThread.start();

        MyThread myThread = new MyThread();
        myThread.start();

        MyThread2 myThread2 = new MyThread2();
        myThread2.start();
        Thread.sleep(3000);
        myThread2.stopThread();
        myThread2.join();
        System.out.println("main thread is over");
    }
}
